package net.zjitc.service.impl;

import net.zjitc.mapper.ProductMapper;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.ProductExample;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/29 10:25
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Product> findAll() {
        return productMapper.selectByExample(null);
    }

    @Override
    public Product findById(String id) {

        return productMapper.selectByPrimaryKey(id);
    }



    @Override
    public List<Product> findByName() {
        ProductExample example = new ProductExample();
        example.or().andNameLike("%java%");
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> findByNameAndCid() {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andCidEqualTo(3);
        criteria.andNameLike("%java%");
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> findByNameAndCidOrOtherCid() {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andCidEqualTo(3);
        criteria.andNameLike("%java%");
        ProductExample.Criteria criteria2= example.createCriteria();
        criteria2.andCidEqualTo(1);
        example.or(criteria2);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> findAllByPrice() {
        ProductExample example = new ProductExample();
        example.setOrderByClause("price desc");

        return productMapper.selectByExample(example);

    }
}
